package com.iot.aliyun.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.iot.aliyun.handler.ResultHandler;
import com.iot.commonutils.utils.Result;
import org.springframework.stereotype.Component;

//产品管理工具类
@Component
public class ProductUtil {

    //公共参数

    String regionId="cn-shanghai";//注册地ID

    String accessKeyId="LTAI4G1NEHgpt3QYNyjUyVaE";

    String secret="8Z9VP1S7KfeWDWdF6gm9Djnuy03w0m";

    String iotInstanceId;//企业实例ID

    String sysDomain="iot."+regionId+".aliyuncs.com";//系统域名

    String sysVersion="2018-01-20";//系统版本

    /**
     *
     * @param NodeType 产品的节点类型
     * @param ProductName 产品名称
     * @param DataFormat 数据格式
     * @param Description 产品的描述信息
     * @param Id2 是否使用ID2认证方式
     * @param ProtocolType 设备接入网关的协议类型
     * @param NetType 连网方式
     * @param JoinPermissionId LoRaWAN入网凭证ID。连网方式选择为LORA时，该参数必需
     * @param ResourceGroupId 产品所属资源组ID
     * @param AuthType 产品下的设备接入物联网平台的认证方式
     * @param CategoryKey 产品品类的标识符
     * @param PublishAuto 是否在产品创建后自动发布物模型
     * @param ValidateType 数据校验级别
     * @return
     */
    public Result CreateProduct(Integer NodeType,String ProductName, Integer DataFormat, String Description,
                                Boolean Id2,String ProtocolType, String NetType,String JoinPermissionId,
                                String ResourceGroupId,String AuthType, String CategoryKey,
                                Boolean PublishAuto,Integer ValidateType
                                ){
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("CreateProduct");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("NodeType",NodeType+"");
        request.putQueryParameter("ProductName", ProductName);
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        request.putQueryParameter("DataFormat", DataFormat+"");
        request.putQueryParameter("Description", Description);
        request.putQueryParameter("AliyunCommodityCode","iothub_senior");//产品的版本类型（唯一且固定）
        request.putQueryParameter("Id2",Id2+"");
        request.putQueryParameter("ProtocolType",ProtocolType);
        request.putQueryParameter("NetType", NetType);
        request.putQueryParameter("JoinPermissionId",JoinPermissionId);
        request.putQueryParameter("ResourceGroupId",ResourceGroupId);
        request.putQueryParameter("AuthType", AuthType);
        request.putQueryParameter("CategoryKey",CategoryKey);
        request.putQueryParameter("PublishAuto", PublishAuto+"");
        request.putQueryParameter("ValidateType", ValidateType+"");
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        }  catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }

    /**
     * 调用该接口修改指定产品的信息
     * @param ProductKey 阿里云为产品生成的唯一标识
     * @param ProductName 修改后的产品名称
     * @param Description 产品描述
     * @return
     */
    public Result UpdateProduct(String ProductName,String ProductKey,String Description) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("UpdateProduct");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("ProductName", ProductName);
        request.putQueryParameter("ProductKey", ProductKey);
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        request.putQueryParameter("Description", Description);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        }  catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }

    /**
     * 调用该接口查看所有产品列表
     * @param CurrentPage 指定显示返回结果中的第几页
     * @param PageSize 指定返回结果中每页显示的产品数量，最大值是200
     * @param AliyunCommodityCode 指定要查看的产品类型（物模型版：iothub_senior，非物模型版iothub）
     * @param ResourceGroupId 产品所属资源组ID
     * @return
     */
    public Result QueryProductList(Integer CurrentPage,Integer PageSize,
                                   String AliyunCommodityCode,String ResourceGroupId){
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("QueryProductList");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("CurrentPage", CurrentPage+"");
        request.putQueryParameter("PageSize", PageSize+"");
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        request.putQueryParameter("ResourceGroupId", AliyunCommodityCode);
        request.putQueryParameter("AliyunCommodityCode", ResourceGroupId);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }

    /**
     * 调用该接口查询指定产品的详细信息
     * @param ProductKey 产品的唯一标识
     * @return
     */
    public Result QueryProduct(String ProductKey) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("QueryProduct");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("ProductKey", ProductKey);
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }

    /**
     * 调用该接口删除指定产品
     * @param ProductKey 产品的唯一标识
     * @return
     */
    public Result DeleteProduct(String ProductKey) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("DeleteProduct");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("ProductKey", ProductKey);
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }

    /**
     * 调用该接口为指定产品创建标签（最多能为指定产品创建10个标签）
     * @param ProDuctKey 产品唯一标识
     * @param ProductTagKeys 产品标签名称
     * @param ProductTagValues 产品标签值
     * @return
     */
    public Object CreateProductTags(String ProDuctKey,String ProductTagKeys,
                                    String ProductTagValues){
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("CreateProductTags");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("ProductTag.1.TagKey", ProductTagKeys);
        request.putQueryParameter("ProductTag.1.TagValue", ProductTagValues);
        request.putQueryParameter("ProductKey", ProDuctKey);
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }

    /**
     * 调用该接口更新产品标签（最多十个）
     * @param ProductKey 产品的唯一标识
     * @param ProductTagKeys 修改后的产品标签名称（传入标签的TagKey必须是已存在的标签key，传入标签的TagKey不能重复）
     * @param ProductTagValues 修改后的产品标签值
     * @return
     */
    public Object UpdateProductTags(String ProductKey,String ProductTagKeys,
                                    String ProductTagValues){
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("UpdateProductTags");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("ProductKey", ProductKey);
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        request.putQueryParameter("ProductTag.1.TagKey", ProductTagKeys);
        request.putQueryParameter("ProductTag.1.TagValue", ProductTagValues);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }

    /**
     * 调用该接口删除产品标签（最多十个）
     * @param ProductKey 产品唯一标识
     * @param ProductTagKeys 要删除的标签名称
     * @return
     */
    public Object DeleteProductTags(String ProductKey,String[] ProductTagKeys){
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(sysDomain);
        request.setSysVersion(sysVersion);
        request.setSysAction("DeleteProductTags");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("ProductKey", ProductKey);
        for(int i=0;i<ProductTagKeys.length;i++){
            request.putQueryParameter("ProductTagKey."+(i+1), ProductTagKeys[i]);
        }
        request.putQueryParameter("IotInstanceId", iotInstanceId);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return ResultHandler.dataResultHandler(response.getData());
        }catch (ClientException e) {
            e.printStackTrace();
        }
        return Result.error("阿里云底层处理错误");
    }
    
}

